//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
    data: {
        userInfo: {},
        unReadMsgNum: 0,
        modulList: [{
            id: '1',
            name: "实时数据",
            link: 'goList',
            icon: 'clock_fill'
        }, {
            id: '5',
            name: "用量查询",
            link: 'goTotal',
            icon: 'dynamic_fill'
        }, {
            id: '3',
            name: "读数查询",
            link: 'goList',
            icon: 'createtask_fill'
        }, {
            id: '4',
            name: "单位查询",
            link: 'goList',
            icon: 'homepage_fill'
        }, {
            id: '2',
            name: "水表查询",
            link: 'goList',
            icon: 'barrage_fill'
        }, {
            id: '6',
            name: "现场执法",
            link: 'goMsgSceneList',
            icon: 'camera_fill'
        }],
        roleList: [],
        imgUrls: [],
        indicatorDots: false,
        autoplay: true,
        interval: 5000,
        duration: 1000
    },
    goMsgSceneList(e) {
        let name = e.currentTarget.dataset.name;
        let url = `/pages/scene-list/scene-list?name=${name}`
        wx.navigateTo({
            url
        });
    },
    goMine() {
        let url = '/pages/mine/mine'
        wx.navigateTo({
            url
        });
    },
    goList(e) {
        let name = e.currentTarget.dataset.name;
        console.log(e)
        let url = `/pages/list/list?name=${name}`
        if (name == "实时数据") {
            url = `/pages/list/list?name=${name}&flag=true`
        }

        wx.navigateTo({
            url
        });
    },
    goTotal(e) {
        let name = e.currentTarget.dataset.name;
        let url = `/pages/sum-line/sum-line?name=${name}`
        wx.navigateTo({
            url
        });
    },
    goScene() {
        let url = '/pages/scene-list/scene-list'
        wx.navigateTo({
            url
        });
    },

    handleClose() {
        this.setData({
            isShowNotice: false
        });
    },
    goNoticeDetail(e) {
        console.log(e, '');
        let _id = e.currentTarget.dataset.id;
        if (!_id) {
            return;
        }
        let url = `/pages/msg-detail/msg-detail?id=${_id}&name=公告`

        wx.navigateTo({
            url
        });

    },
    onShow: function(options) {
        let pages = getCurrentPages();
        let curPage = pages[pages.length - 1];
        app.checkBind(curPage.route);

        wx.getUserInfo({
            success: userData => {
                app.globalData.userInfo = userData.userInfo
                this.setData({
                    userInfo: userData.userInfo
                })
            }
        })

        if (!this.data.userInfo.nickName) {
            this.setData({
                userInfo: app.globalData.userInfo
            })
        }

        // wx.showLoading({
        //     title: '加载中...'
        // })
        // 轮播公告、获取授权模块
        let _this = this;
        app.ajax({
            url: `${config.httpUrl}log/roleResList`,
            method: 'get',
            callback: function(error, roleData) {
                let dataList = roleData.data.data;
                let roleArr = Object.keys(dataList[1]);
                let roleArrList = _this.data.modulList.filter((item) => {
                    return roleArr.indexOf(item.id) !== -1
                })
                _this.setData({
                    unReadMsgNum: +dataList[0].unReadMsgNum,
                    roleList: roleArrList
                })
                app.globalData.roleList = Object.values(dataList[1]);
                app.ajax({
                    url: `${config.httpUrl}ad/findList`,
                    method: 'get',
                    callback: function(error, data) {
                        // wx.hideLoading()
                        _this.setData({
                            imgUrls: data.data
                        });
                    }
                })
            }
        })
    }
})